package com.jadefashion.controller;

import com.alibaba.fastjson.JSON;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.Role;
import com.jadefashion.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询显示角色数据
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            Map map = roleService.queryPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 添加角色信息
     *
     * @param map
     * @return
     */
    @RequestMapping("addRole")
    public Result addRole(@RequestBody HashMap<String, String> map) {
        try {
            //获取前端传递的数据
            Role role = JSON.parseObject(map.get("formData"), Role.class);
            List<Integer> permissionIds = JSON.parseArray(map.get("permissionIds"), Integer.class);
            List<Integer> menuIds = JSON.parseArray(map.get("menuIds"), Integer.class);
            roleService.addRole(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }


    //  TODO:  更新角色对应权限数据
    @RequestMapping("/updatePermissionForRoleByDetail")
    public Result updatePermissionForRoleByDetail(@RequestBody HashMap<String, String> map) {
        try {
            //获取删除ids
            List<Integer> deleteIds = JSON.parseArray(map.get("deleteIds"), Integer.class);

            //获取添加ids
            List<Integer> addIds = JSON.parseArray(map.get("addIds"), Integer.class);
            //获取group id
            Integer currentRowId = Integer.parseInt(map.get("currentRowId"));

            roleService.updatePermissionForRoleByDetail(deleteIds, addIds, currentRowId);
            return new Result(true, MessageConstant.UPDATE_PERMISSION_FOR_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_PERMISSION_FOR_ROLE_FAIL);
        }
    }

    //  TODO:  更新角色对应菜单数据
    @RequestMapping("/updateMenuForRoleByDetail")
    public Result updateMenuForRoleByDetail(@RequestBody HashMap<String, String> map) {
        try {
            //获取删除ids
            List<Integer> deleteIds = JSON.parseArray(map.get("deleteIds"), Integer.class);

            //获取添加ids
            List<Integer> addIds = JSON.parseArray(map.get("addIds"), Integer.class);
            //获取group id
            Integer currentRowId = Integer.parseInt(map.get("currentRowId"));

            roleService.updateMenuForRoleByDetail(deleteIds, addIds, currentRowId);
            return new Result(true, MessageConstant.UPDATE_MENU_FOR_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MENU_FOR_ROLE_FAIL);
        }
    }

    //  TODO:   编辑表单角色
    @RequestMapping("/updateRole")
    public Result editRole(@RequestBody Role role) {
        try {
            roleService.updateRole(role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //TODO:    删除角色
    @RequestMapping("/deleteRole")
    public Result deleteRole(@RequestBody HashMap<String, Object> map) {
        try {
            Integer id = (Integer) map.get("id");
            roleService.deleteRole(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            //若是此角色被用户引用即不能删除
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //  TODO:   查询所有role
    @RequestMapping("/queryAllRole")
    public Result queryAllRole() {
        try {
            List<Role> list_role = roleService.queryAllRole();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list_role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //    TODO: 查询user对应role
    @RequestMapping("/queryRoleForUser")
    public Result queryRoleForUser(@RequestBody HashMap<String, String> map) {

        try {
            Integer id = Integer.parseInt(map.get("id"));
            List<Role> list_role = roleService.queryRoleForUser(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list_role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
